package ln.fxspringarchetype.controller.student;

import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import ln.fxspringarchetype.controller.BaseFxController;
import ln.fxspringarchetype.vo.StudentVO;

import java.io.*;
import java.net.URI;
import java.util.Objects;

public class EditStudentController extends BaseFxController {
    private static final String default_photo_path = "/img/default-photo.jpg";
    private static final File default_photo_file = new File(
            URI.create(String.valueOf(AddStudentController.class.getResource(default_photo_path))));

    private final StudentVO studentVO = new StudentVO();
    private final FileChooser fileChooser = new FileChooser();
    private final ObjectProperty<File> photoFileProperty = new SimpleObjectProperty<>();
    @FXML
    private Label idLabel;
    @FXML
    private BorderPane formPane;
    @FXML
    private TextField nameField;
    @FXML
    private DatePicker birthdayField;
    @FXML
    private ChoiceBox<String> genderField;
    @FXML
    private ImageView photoView;
    @FXML
    private Button photoButton;

    public StudentVO getStudentVO() {
        return studentVO;
    }

    @Override
    public void setCellValueFactories() {

    }

    @Override
    public void setCellFactories() {

    }

    @Override
    public void createBindings() {
        idLabel.textProperty().bind(
                Bindings.createStringBinding(
                        () -> Integer.toString(studentVO.getId()), studentVO.idProperty()));
        nameField.textProperty().bindBidirectional(studentVO.nameProperty());
        genderField.valueProperty().bindBidirectional(studentVO.genderProperty());
        birthdayField.valueProperty().bindBidirectional(studentVO.birthdayProperty());
        photoView.imageProperty().bind(
                Bindings.createObjectBinding(
                        () -> {
                            InputStream inputStream = new ByteArrayInputStream(new byte[0]);
                            if (Objects.nonNull(studentVO.getPhoto())) {
                                inputStream = new ByteArrayInputStream(studentVO.getPhoto());
                            }
                            return new Image(inputStream);
                        }, studentVO.photoProperty()));
        //这里用了addListener，不想把studentVO.photo变成被绑定变量，因为fromModel方法会修改成员
        photoFileProperty.addListener((observable, oldFile, newFile) -> {
            byte[] bytes = new byte[0];
            if (Objects.nonNull(newFile) && newFile.exists()) {
                try {
                    FileInputStream fileInputStream = new FileInputStream(newFile);
                    bytes = fileInputStream.readAllBytes();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            studentVO.setPhoto(bytes);
        });
    }

    @Override
    public void initLayout() {

    }

    @Override
    public void loadData() {

    }

    @FXML
    private void choosePhoto() {
        photoFileProperty.setValue(
                Objects.requireNonNullElse(
                        fileChooser.showOpenDialog(Window.getWindows().get(0)),
                        default_photo_file
                )
        );
    }
}
